angular.module('mine', [])
    .controller('mineCtrl', ['$scope', 'httpService', '$localStorage', 'role', '$state', '$rootScope', 'alertmsg', '$stateParams', function($scope, httpService, $localStorage, role, $state, $rootScope, alertmsg, $stateParams) {
        $scope.rightSideHeight = $(window).height() + 100;

        $scope.user_type = function() {

        }

        function setCorrectHeight(rightSideHeight) {
            if (rightSideHeight < $(window).height()) {
                if ($(window).height() < 765) {
                    return $(window).height() + 100;
                } else {
                    return $(window).height();
                }
            } else {
                return rightSideHeight + 300;
            }
        }

        if ($localStorage.user) {
            $scope.mineListByRole = role.get($localStorage.user);
            getPersion($localStorage.user.id);
        }

        $scope.brandmanagerlist = [
            { name: '邀请成员', icon: 'yaoqing', params: 'invite' },
            { name: '管理成员', icon: 'guanli', params: 'management' }
        ];

        $scope.offcanvas = function() {
            $('.row-offcanvas').toggleClass('active');
        };

        // console.log("$localStorage.user && $localStorage.user.company_id", $localStorage.user && $localStorage.user.company_id)
        if ($localStorage.user && $localStorage.user.company_id) {
            $scope.defaultlist = [
                { name: '个人资料', icon: 'ziliao', params: 'zoom,uploadCtrl,personal' },
                { name: '账户设置', icon: 'anquan', params: 'setting' },
                { name: '通讯目录', icon: 'tongxunlu', params: 'contacts' }
            ];
            if(($localStorage.user.user_type == 'finance' && $localStorage.user.company_id) || !$localStorage.user.company_id) {
                $scope.defaultlist = $scope.defaultlist.concat({ name: '发票管理', icon: 'fapiao', params: 'invoice' });
            }
            if ($localStorage.user.role == 1 && $localStorage.user.user_type == 'manager') {
                var obj = [
                    { name: '渠道管理', icon: 'qudao', params: 'canal' },
                    // { name: '品牌管理', icon: 'pinpai', params: 'enterprise' },
                    { name: '品牌设置', icon: 'pinpai', params: 'brandset' },
                    { name: '更多功能', icon: 'more', params: 'functions' }
                ];
                angular.forEach(user.addons, function(v, k) {
                    if(v.serial_number == '5mutian_brand_manage') {
                        obj.splice(1, 0, { name: '品牌管理', icon: 'pinpai', params: 'enterprise' });
                    }
                });
                $scope.defaultlist = $scope.defaultlist.concat(obj);
            }
        } else {
            $scope.defaultlist = [
                { name: '个人资料', icon: 'ziliao', params: 'zoom,uploadCtrl,personal' },
                { name: '账户设置', icon: 'anquan', params: 'setting' },
                { name: '发票管理', icon: 'fapiao', params: 'invoice' }
            ];
        }

        $scope.select = function(r) {
            if (r.name == '我的钱包') {
                if (($state.includes('mine.recharge') || $state.includes('mine.cash'))) {
                    return true;
                } else {
                    return false; }
            } else if (r.name == '我的订单') {
                if ($state.includes('mine.orderdetail') || ($state.includes('mine.changeorder') && !$stateParams.isreview)) {
                    return true;
                } else {
                    return false; }
            } else if (r.name == '审核订单') {
                if ($state.includes('mine.revieworder') || ($state.includes('mine.changeorder') && $stateParams.isreview == '1')) {
                    return true;
                } else {
                    return false; }
            } else if (r.name == '更多功能') {
                if ($state.includes('mine.functions_add') || $state.includes('mine.functions_detail') || $state.includes('mine.node')) {
                    return true;
                } else {
                    return false; }
            } else if (r.name == '发票管理') {
                if($state.includes('mine.invoice') || $state.includes('mine.newInvoice') || $state.includes('mine.invoiceDetail')) {
                    return true;
                } else {
                    return false;
                }
            } else if (r.name == '品牌设置') {
                if($state.includes('mine.brandadd')) {
                    return true;
                } else {
                    return false;
                }
            } else if (r.name == '品牌管理') {
                if($state.includes('mine.brandnew')) {
                    return true;
                } else {
                    return false;
                }
            }
            $scope.rightSideHeight = setCorrectHeight($('#right-side').height()) - 70;
        };

        function getPersion(id) {
            if (id) {
                httpService.query('web/users/' + id).then(function(res) {
                    if (res.code == 200) {
                        $localStorage.user = res.user;
                        user = res.user;
                        $rootScope.user = res.user;
                        if(user && user.company_id) {
                            $scope.getRoles(user.company_id);
                        }
                        
                    }
                });
            } else {
                $state.go('login');
            }
        };
        
        $scope.getRoles = function(company_id) {
            httpService.query('web/companies/' + company_id + '/roles').then(function(res) {
                if(res.code == 200) {
                    $rootScope.roles = res.roles;
                }
            });
        };
        
    }]);
